package com.hq.fxh.No1_Infromation.Video.Activity;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.imageLoader.video.VideoItem;
import com.hq.fxh.imageLoader.video.VideoPicker;

import java.util.ArrayList;


public class VideoOpenSelectActivity extends BaseActivity implements View.OnClickListener {

    protected ImageView imageVideoSelectBack;
    protected LinearLayout baseLayoutBack;
    protected VideoView videoViewVideoSelect;
    protected TextView tvVideoSelectUpView;
    String videoPath;
    MediaController mediaco;
    private VideoPicker videoPicker;
    private ArrayList<VideoItem> selectedVideos;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_open_video;
    }

    @Override
    protected void initView() {
        imageVideoSelectBack = (ImageView) findViewById(R.id.image_VideoSelect_back);
        baseLayoutBack = (LinearLayout) findViewById(R.id.base_layout_back);
        baseLayoutBack.setOnClickListener(VideoOpenSelectActivity.this);
        videoViewVideoSelect = (VideoView) findViewById(R.id.videoView_VideoSelect);
        videoViewVideoSelect.setOnClickListener(VideoOpenSelectActivity.this);
        tvVideoSelectUpView = (TextView) findViewById(R.id.tv_VideoSelect_UpView);
        tvVideoSelectUpView.setOnClickListener(VideoOpenSelectActivity.this);
    }

    @Override
    protected void initData() {
        mediaco = new MediaController(this);
        try {
            videoPicker = VideoPicker.getInstance();
            selectedVideos = videoPicker.getSelectedVideos();
            videoPath = selectedVideos.get(0).path;
            //    videoPath = getIntent().getExtras().getString("key");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (videoPath == null) {
            LogUtils.e("没有接受到视频");
        } else {
            videoViewVideoSelect.setVideoPath(videoPath);
            videoViewVideoSelect.setMediaController(mediaco);
            mediaco.setMediaPlayer(videoViewVideoSelect);
            //让VideiView获取焦点并且开始播放视频
            videoViewVideoSelect.requestFocus();
            videoViewVideoSelect.start();
        }
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.base_layout_back) {
            finish();

        } else if (view.getId() == R.id.videoView_VideoSelect) {

        } else if (view.getId() == R.id.tv_VideoSelect_UpView) {
            Intent intent = new Intent(this, VideoUpdateActivity.class);
            intent.putExtra("path", videoPath);
            startActivity(intent);
            finish();
        }
    }
}
